package com.kly.user.task.v2;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.user.db.dao.UserChatterMappingDao;
import com.kly.user.db.dao.UserConversationDao;
import com.kly.user.db.dao.UserConversationNumDao;
import com.kly.user.db.entity.UserChatterMappingEntity;
import com.kly.user.db.entity.UserConversationNumEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * chatter当日会话计数
 */
@Service
public class ConversationTask {

    private static Log logger = LogFactory.getLog(ConversationTask.class);

    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;
    private String localIP = NetUtil.getLocalhostStr();
    @Resource
    UserConversationDao userConversationDao;
    @Resource
    UserConversationNumDao userConversationNumDao;
    @Resource
    UserChatterMappingDao userChatterMappingDao;


    @Scheduled(fixedDelay = 1000 * 3600)
    public void run() {
        if (!taskJobExecutor.contains(localIP)) {
            logger.info("=============ConversationTask, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, taskJobExecutor);
            return;
        }

        final List<UserChatterMappingEntity> userChatterMappingEntities = userChatterMappingDao.selectAllChatters();
        if (CollectionUtils.isEmpty(userChatterMappingEntities)) {
            return;
        }

        final DateTime beginOfToday = DateUtil.beginOfDay(new Date());
        for (UserChatterMappingEntity chatterModel : userChatterMappingEntities) {
            final boolean isNewBindModel = chatterModel.getGmtCreate().after(beginOfToday);
            if (isNewBindModel) {
                final int activeSessionNumToday = userConversationDao.countByGroupId(chatterModel.getModelUserCode(), chatterModel.getGmtCreate());
//                final int newChatNumToday = userConversationDao.countByGroupId2(chatterModel.getModelUserCode(), date);
                addOrUpdateChatterConversation(chatterModel, activeSessionNumToday, activeSessionNumToday);
            } else {
                final int activeSessionNumToday = userConversationDao.countByGroupId(chatterModel.getModelUserCode(), beginOfToday);
                final int newChatNumToday = userConversationDao.countByGroupId2(chatterModel.getModelUserCode(), beginOfToday);
                addOrUpdateChatterConversation(chatterModel, activeSessionNumToday, newChatNumToday);
            }


        }
    }

    private void addOrUpdateChatterConversation(UserChatterMappingEntity chatterModel, Integer activeSession, Integer newChatNum) {
        final int todayDate = Integer.parseInt(DateUtil.format(new Date(), "yyyyMMdd"));

        UserConversationNumEntity userConversationNum = userConversationNumDao.getUserConversationNum(chatterModel.getChatterUserCode(), chatterModel.getModelUserCode(), todayDate);
        if (userConversationNum == null) {
            userConversationNum = new UserConversationNumEntity();
            userConversationNum.setChatterUserCode(chatterModel.getChatterUserCode());
            userConversationNum.setModelUserCode(chatterModel.getModelUserCode());
            userConversationNum.setAllSessionNum(activeSession);
            userConversationNum.setNewSessionNum(newChatNum);
            userConversationNum.setAddConversationDate(todayDate);
            userConversationNumDao.insert(userConversationNum);
        } else {
//            if (!userConversationNum.getAllSessionNum().equals(activeSession) || !userConversationNum.getNewSessionNum().equals(newChatNum)) {
//                userConversationNum.setAllSessionNum(activeSession);
//                userConversationNum.setNewSessionNum(newChatNum);
//                userConversationNumDao.updateByPrimaryKey(userConversationNum);
//            }
            if (activeSession > userConversationNum.getAllSessionNum() || newChatNum > userConversationNum.getNewSessionNum()) {
                userConversationNum.setAllSessionNum(activeSession);
                userConversationNum.setNewSessionNum(newChatNum);
                userConversationNumDao.updateByPrimaryKey(userConversationNum);
            }
        }
    }
}
